package com.powernode.business.service.impl;

import com.powernode.business.constant.SnConstant;
import com.powernode.business.domain.Car;
import com.powernode.business.domain.Customer;
import com.powernode.business.domain.Rent;
import com.powernode.business.mapper.CarMapper;
import com.powernode.business.mapper.CustomerMapper;
import com.powernode.business.mapper.RentMapper;
import com.powernode.business.utils.CreateSnUtil;
import com.powernode.business.vo.CheckVo;
import com.powernode.common.core.domain.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.powernode.business.mapper.CheckMapper;
import com.powernode.business.domain.Check;
import com.powernode.business.service.CheckService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Service
public class CheckServiceImpl implements CheckService{

    @Resource
    private CheckMapper checkMapper;

    @Autowired
    private RentMapper rentMapper;

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private CreateSnUtil createSnUtil;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return checkMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Check record) {
        return checkMapper.insert(record);
    }

    @Override
    public int insertSelective(Check record) {
        return checkMapper.insertSelective(record);
    }

    @Override
    public Check selectByPrimaryKey(Integer id) {
        return checkMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Check record) {
        return checkMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Check record) {
        return checkMapper.updateByPrimaryKey(record);
    }

    /**
     * 根据出租单号查询相关信息
     * 1.根据出租单号查询状态
     * 2.查询用户表
     * 3.查询汽车表
     * 4.查询租车表
     * 5.生成检查单号 组装检查对象
     * 6.返回
     * @param rentSn
     * @return
     */
    @Override
    public CheckVo checkRentSn(String rentSn) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Rent rent = rentMapper.selectByRentSn(rentSn);
        System.out.println(rent);
        if (ObjectUtils.isEmpty(rent)){
            throw new IllegalArgumentException("当前出租单不存在，请核对后再输入");
        }
        if (rent.getRentStatus().equals(1)){
            throw new IllegalArgumentException("此出租单已归还，请勿重复入库");
        }
        //查车辆表
        Car car = carMapper.selectCarByCarNum(rent.getCarNumber());
        //查用户表
        Customer customer = customerMapper.selectByIdentity(rent.getIdentity());

        Check check = new Check();
        String checkSn = createSnUtil.createSn(SnConstant.CHECK_SN_PREFIX);
        check.setCheckSn(checkSn);
        check.setRentSn(rent.getRentSn());
        check.setCheckDate(new Date());
        check.setOpername(loginUser.getUsername());
        //组装对象
        return CheckVo.builder()
                .customer(customer)
                .car(car)
                .rent(rent)
                .check(check)
                .build();
    }

    /**
     * 车辆入库
     * 1.先拿到租车单号
     * 2.把租车单状态修改
     * 3.根据租车对象 里面车牌号 查询汽车
     * 4.把车辆装填改为未出租
     * 5.插入检查单
     * @param check
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void carBack(Check check) {
        String rentSn = check.getRentSn();
        Rent rent = rentMapper.selectByRentSn(rentSn);
        //更改为归还
        rent.setRentStatus(1);
        rent.setUpdateTime(new Date());
        rent.setUpdateBy(check.getOpername());
        int i = rentMapper.updateByPrimaryKey(rent);
        if (i > 0){
            //查汽车表
            Car car = carMapper.selectCarByCarNum(rent.getCarNumber());
            //改为未出租
            car.setStatus(0);
            car.setUpdateTime(new Date());
            car.setUpdateBy(check.getOpername());
            int b = carMapper.updateByPrimaryKey(car);
            if (b > 0){
                //插入检查单
                check.setCreateTime(new Date());
                check.setCreateBy(check.getOpername());
                check.setUpdateTime(new Date());
                check.setUpdateBy(check.getOpername());
                check.setDelFlag("0");
                check.setDeptId(rent.getDeptId());
                //插入
                checkMapper.insert(check);
            }
        }
    }


    @Override
    public List<Check> selectcrCheck(Check check, LoginUser loginUser) {
        return checkMapper.selectCheck(check,loginUser);
    }

    /**
     * 删除检查单
     * @param id
     * @param username
     * @return
     */
    @Override
    public int deleteCheck(List<Integer> id, String username) {
        return checkMapper.deleteCheck(id,username);
    }
}
